package com.example.basecore.boot;

import com.example.basecore.context.SpringContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

import static org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON;

/**
 * @author ycd0075
 * @date 2024-01-25 22:29
 */
public class RegistryBeanProcessor implements BeanDefinitionRegistryPostProcessor {

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    // 注入Command
            this.registry(BeanDefinitionBuilder.rootBeanDefinition(CommandRegistry.class).setRole(BeanDefinition.ROLE_APPLICATION)
                    .setScope(SCOPE_SINGLETON).setAutowireMode(AUTOWIRE_CONSTRUCTOR).getBeanDefinition(),registry);

    // 注入Event
            this.registry(BeanDefinitionBuilder.rootBeanDefinition(EventRegistry.class).setRole(BeanDefinition.ROLE_APPLICATION)
                    .setScope(SCOPE_SINGLETON).setAutowireMode(AUTOWIRE_CONSTRUCTOR).getBeanDefinition(),registry);

    // 注入spring上下文
            this.registry(BeanDefinitionBuilder.rootBeanDefinition(SpringContext.class).setRole(BeanDefinition.ROLE_APPLICATION)
                    .setScope(SCOPE_SINGLETON).setAutowireMode(AUTOWIRE_CONSTRUCTOR).getBeanDefinition(),registry);

    /**
     this.registry(BeanDefinitionBuilder.rootBeanDefinition(AutowireStateMachineFactoryRegistry.class)
     .setRole(BeanDefinition.ROLE_APPLICATION)
     .setScope(SCOPE_SINGLETON)
     .setAutowireMode(AUTOWIRE_CONSTRUCTOR)
     .getBeanDefinition(),
     registry);
     **/
}
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {

    }
    private void registry(BeanDefinition beanDefinition, BeanDefinitionRegistry registry) {
        registry.registerBeanDefinition(BeanDefinitionReaderUtils.generateBeanName(beanDefinition, registry), beanDefinition);
    }
}
